package com.road.yishi.admincmd;

import java.io.BufferedWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.road.yishi.base.AdminCmd;

/**
 * @Description :
 * @FileName: AbstractAdminCommand.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月19日 上午11:48:05
 * @Version:V1.00
 */
public abstract class AbstractAdminCommand implements AdminCommand {

	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAdminCommand.class);

	@Override
	public void execute(String[] params, BufferedWriter writer) {
		AdminCmd cmd = this.getClass().getAnnotation(AdminCmd.class);
		try {
			if (!exec(params, writer)) {
				send(writer, "参数不正确" + cmd.desc());
			}
		} catch (Exception e) {
			send(writer, cmd.desc());
			LOGGER.error("cmd:" + cmd.value(), e);
		}
	}

	protected void send(BufferedWriter writer, String str) {
		try {
			writer.write(str);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			LOGGER.error("send error", e);
			e.printStackTrace();
		}
	}

	public abstract boolean exec(String[] params, BufferedWriter writer) throws Exception;

}
